package org.ytor.core.sysapi.dict.logic;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.ytor.common.util.Colls;
import org.ytor.common.util.Strs;
import org.ytor.common.util.bean.Beans;
import org.ytor.core.sqlflow.PersistenceModel;
import org.ytor.core.sqlflow.BaseLogic;
import org.ytor.core.sqlflow.Page;
import org.ytor.core.sysapi.dict.DictCache;
import org.ytor.core.sysapi.dict.model.SysDict;
import org.ytor.core.sysapi.dict.repository.SysDictRepository;
import org.ytor.core.sysapi.dict.req.SysDictItemReq;
import org.ytor.core.sysapi.dict.req.SysDictReq;
import org.ytor.core.sysapi.dict.resp.SysDictItemResp;
import org.ytor.core.sysapi.dict.resp.SysDictResp;
import org.ytor.core.util.Pages;

import java.util.List;

/**
 * created by yangtong on 2025/4/8 16:48:04
 * <br/>
 */
@Service
public class SysDictLogic extends BaseLogic<SysDict, SysDictRepository> {

    public Page<SysDictResp> pageDict(SysDictReq dictReq) {

        Page<SysDict> page = repository.queryPage(w ->
                w.like(Strs.isNotEmpty(dictReq.getDictName()), SysDict::getDictName, dictReq.getDictName())
                        .like(Strs.isNotEmpty(dictReq.getDictCode()), SysDict::getDictCode, dictReq.getDictCode())
                        .eq(SysDict::getType, 0)
        );

        return Pages.transPage(page, item -> {
            SysDictResp resp = new SysDictResp();
            Beans.copyProperties(item, resp);
            return resp;
        });
    }

    public void saveOrUpdateDict(SysDictReq dictReq) {
        SysDict dict = new SysDict();
        Beans.copyProperties(dictReq, dict);
        dict.setType(0);
        repository.insertOrUpdate(dict);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteDict(String id) {
        SysDict sysDict = repository.queryById(id);
        if (sysDict != null) {
            List<SysDict> list = repository.queryList(w -> w.eq(SysDict::getDictCode, sysDict.getDictCode()));
            if (Colls.isNotEmpty(list)) {
                List<String> ids = list.stream().map(PersistenceModel::getId).toList();
                repository.deleteById(ids);
            }

            //使缓存失效
            for (SysDict dict : list) {
                DictCache.invalidDictItem(dict.getDictCode(), dict.getDictItemValue());
            }
        }

    }

    public List<SysDictItemResp> listDictItem(SysDictItemReq dictItemReq) {
        List<SysDict> list = repository.queryList(w ->
                w.eq(SysDict::getDictCode, dictItemReq.getDictCode())
                        .eq(SysDict::getType, 1));

        return list.stream().map(item -> {
            SysDictItemResp resp = new SysDictItemResp();
            Beans.copyProperties(item, resp);
            return resp;
        }).toList();

    }

    public void saveOrUpdateDictItem(SysDictItemReq dictItemReq) {
        SysDict dict = new SysDict();
        Beans.copyProperties(dictItemReq, dict);
        dict.setType(1);
        repository.insertOrUpdate(dict);
        //使缓存失效
        DictCache.invalidDictItem(dictItemReq.getDictCode(), dictItemReq.getDictItemValue());
    }

    public void deleteDictItem(String id) {
        SysDict dictItem = repository.queryById(id);
        if (dictItem != null) {
            repository.deleteById(id);
            //使缓存失效
            DictCache.invalidDictItem(dictItem.getDictCode(), dictItem.getDictItemValue());
        }

    }

    public String parseDictText(String dictCode, String dictItemValue) {
        //1.先从缓存获取
        String dictText = DictCache.getDictItemText(dictCode, dictItemValue);
        if (dictText != null) {
            return dictText;
        }

        //2.缓存中没有，从数据库查询
        SysDict dict = repository.queryOne(w -> w.eq(SysDict::getDictCode, dictCode).eq(SysDict::getDictItemValue, dictItemValue));
        if (dict != null) {
            String dictItemText = dict.getDictItemText();
            DictCache.putDictItemText(dict.getDictCode(), dict.getDictItemValue(), dictItemText);
            return dictItemText;
        }

        //没找到该字典项，返回null
        return null;

    }
}
